// pages/myaddress/myaddress.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    addressid:'',
    dataList:[],
    transferTime:'',
    orderid:'',
    phone:'',
    name:'',
    address:'',
    addressid:''
  },
  // 跳转到物流详情的页面
  logistics(){
    wx.navigateTo({
      url: '/pages/logistics/logistics?expressNumber=' + this.data.dataList.expressNumber,
    })
  },
  // 查询具体地址信息
  getaddressData(){
    var that = this
    wx.request({
      url: getApp().globalData.baseUrl + '/wago/address/selectByID',
      data: {
        addressid:that.data.addressid
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function(res){
        // success
        that.setData({
          phone:res.data.data.phone,
          name:res.data.data.name,
          address:res.data.data.address,
        })
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
  },
  // 提交修改的数据
  submit(){
    var that = this
    wx.request({
      url: getApp().globalData.baseUrl + '/wago/order/updateOrderAddress',
      data: {
        orderid:that.data.orderid,
        addressid:that.data.addressid
      },
      method: 'POST', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function(res){
        // success
        if(res.data.flag){
          wx.showToast({
            title: res.data.message,
          })
         wx.navigateTo({
           url: '/pages/applicationStatus/applicationStatus',
         })
          
        }
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
  },
  // 时间格式转换
  transferTime(value) {
    var date = new Date(value);
    var Y = date.getFullYear() + "-";
    var M = (date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1) + "-";
    var D = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
    var ho = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
    var mi = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
    var se = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
    var time = Y + M + D + ' ' + ho + ":" + mi + ":" + se;
    return time;
  },
  tochangeEdit(e){
    wx.navigateTo({
      url: '/pages/orderEditAddress/orderEditAddress?orderid=' + e.currentTarget.dataset.orderid
      +'&addressid=' + e.currentTarget.dataset.addressid
    })
  },
 
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var mydatalist = JSON.parse(options.item)
    this.setData({
      dataList:mydatalist,
      phone:mydatalist.phone,
      name:mydatalist.name,
      address:mydatalist.address,
      transferTime:this.transferTime(mydatalist.ctrate_time),
      orderid:mydatalist.orderid,
      addressid:mydatalist.addressid,
      sta:JSON.parse(options.sta)
    })
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    if(this.data.addressid != ''){
      this.getaddressData()   //查询address具体信息
    }
    //当前页面
    let pages = getCurrentPages();
    
    //上一页面
    let prevPage = pages[pages.length - 2];
    
    //将数值信息赋值给上一页面sta变量
    prevPage.setData({
      sta: this.data.sta
    });
  },

})